package fr.xebia;

import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.resource.ByteArrayResource;

import xke.xwfc.biz.CallGirlBackend;
import xke.xwfc.biz.domain.CallGirl;
import xke.xwfc.biz.domain.Picture;

public class CataloguePage extends BasePage {

	private static final long serialVersionUID = 1L;

	private ListView callGirlsListView;

	@SuppressWarnings("unchecked")
	public CataloguePage() {
		super(null);
		addCallGirls(CallGirlBackend.find(null, 0, 0, null, 0));
	}

	@SuppressWarnings("unchecked")
	public CataloguePage(List<CallGirl> callGirls) {
		super(null);
		addCallGirls(callGirls);
	}

	private void addCallGirls(List<CallGirl> callGirls) {
		callGirlsListView = new ListView("callGirls", callGirls) {

			private static final long serialVersionUID = 1L;

			public void populateItem(final ListItem listItem) {
				final CallGirl callGirl = (CallGirl) listItem.getModelObject();
				Picture mainPicture = callGirl.getMainPicture();
				ByteArrayResource imageResource = new ByteArrayResource(mainPicture.getMimeType(), mainPicture.getImage());
				listItem.add(new Image("photo", imageResource));
				listItem.add(new Label("prenom", callGirl.getFirstName()));
				listItem.add(new Label("hairColor", callGirl.getHairColor().toString()));
				listItem.add(new Label("age", String.valueOf(callGirl.getAge())));
				listItem.add(new Label("standardPrice", String.valueOf(callGirl.getStandardPrice())));
				listItem.add(new Label("specialPrice", String.valueOf(callGirl.getSpecialPrice())));
				
				Link detailsLink = new Link("addToCartLink") {

					private static final long serialVersionUID = 1L;

					public void onClick() {
						AddToCartPage page = new AddToCartPage(callGirl.getFirstName());
						setResponsePage(page);
					}

				};
				listItem.add(detailsLink);
			}
		};
		add(callGirlsListView);
	}

}
